<?php

include 'DBconn.php';

class CombinationMaster {

	public function getAllCountryCurrency() 
	{
		$ret = array();
		$result1 = mysql_query("Select * from country_header where activestatus=1 ORDER BY country_name") or die(mysql_error());
		while ($row1 = mysql_fetch_object($result1)) 
		{
			$tmp = new VOCountryCurrency();
			$tmp->country_serialnumber = (int) $row1->serialnumber;
            $tmp->country_name = $row1->country_name;
            $tmp->cre_userid_fk = (int) $row1->cre_userid_fk;
            $tmp->cre_datetime = date("d-m-Y", strtotime($row1->cre_datetime));
            $tmp->activestatus = (boolean) $row1->activestatus;
			
			$result2 = mysql_query("Select * from currency_header where country_serialnumber_fk='$row1->serialnumber'") or die(mysql_error());
			while ($row2 = mysql_fetch_object($result2)) 
			{
			    $tmp->currency_serialnumber = (int) $row2->serialnumber;
				$tmp->currency_name = $row2->currency_name;
				
				$result3 = mysql_query("SELECT currency_value from currency_detail WHERE activestatus ='1' and to_currency_fk='$row2->serialnumber' ORDER BY detserialnumber DESC LIMIT 1") or die(mysql_error());
				$row3 = mysql_fetch_row($result3);
				$tmp->currency_value =(double)$row3[0];
			}
		  $ret[] = $tmp;
		}	
	 return $ret;
	}
	
	public function addNewCountryCurrency($VObject)
	{
	  //Add Country
		$query = "Insert into country_header(country_name,cre_userid_fk,cre_datetime,activestatus)values('$VObject->country_name','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
		$s = mysql_query($query) or die(mysql_error());	 
		$last_country_id = mysql_insert_id();
	  //Add Currency
		$query1 = "INSERT INTO currency_header(country_serialnumber_fk,currency_name,currency_symbol,currency_code,cre_userid,cre_datetime,activestatus) values('$last_country_id','$VObject->currency_name','symbole','code','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
		$s1 = mysql_query($query1) or die(mysql_error());	
		$last_currency_id = mysql_insert_id();	
	  //Add Currency Value
	    $query2 = "Insert into currency_detail(to_currency_fk,currency_value,currency_todate,cre_userid,cre_datetime,activestatus) values ('$last_currency_id','$VObject->currency_value','$VObject->cre_datetime','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
        $s2 = mysql_query($query2) or die(mysql_error());
	 return $this->getAllCountryCurrency();
	}
	
	public function editCountryCurrency($VObject)
	{
	  //Update Country
		$query = "Update country_header set country_name='$VObject->country_name' where serialnumber='$VObject->country_serialnumber'";
		$s = mysql_query($query) or die(mysql_error());	 
	  //Update Currency
		$query1 = "Update currency_header set currency_name='$VObject->currency_name' where serialnumber='$VObject->currency_serialnumber'";
		$s1 = mysql_query($query1) or die(mysql_error());	
	  //Update Currency Value
	    $query2 = "Insert into currency_detail(to_currency_fk,currency_value,currency_todate,cre_userid,cre_datetime,activestatus) values ('$VObject->currency_serialnumber','$VObject->currency_value','$VObject->cre_datetime','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
        $s2 = mysql_query($query2) or die(mysql_error());
	 return $this->getAllCountryCurrency();
	}
	
	public function getAllStates()
	{
		$ret = array();
		$result1 = mysql_query("Select sat.serialnumber,sat.country_fk,sat.state_name,sat.cre_userid_fk,sat.cre_datetime,sat.activestatus,con.country_name from state_header as sat LEFT JOIN country_header as con ON con.serialnumber=sat.country_fk where sat.activestatus=1 ORDER BY con.country_name") or die(mysql_error());
		while ($row1 = mysql_fetch_object($result1)) 
		{
			$tmp = new VOCountryState();
			$tmp->serialnumber = (int) $row1->serialnumber;
            $tmp->country_fk = (int) $row1->country_fk;
			$tmp->country_name = $row1->country_name;
            $tmp->state_name = $row1->state_name;
            $tmp->cre_userid_fk = (int) $row1->cre_userid_fk;
            $tmp->cre_datetime = $row1->cre_datetime;
            $tmp->activestatus = (boolean) $row1->activestatus;
            $ret[] = $tmp;
		}
	 return $ret;
	}
	
	public function addNewCountryState($VObject)
	{
		//Add State
		$query = "Insert into state_header(country_fk,state_name,cre_userid_fk,cre_datetime,activestatus) values('$VObject->country_fk','$VObject->state_name','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
		$s = mysql_query($query) or die(mysql_error());	 
		return $this->getAllStates();
	}
	
	public function editCountryState($VObject)
	{
		//Update State
		$query = "Update state_header set country_fk='$VObject->country_fk',state_name='$VObject->state_name' where serialnumber='$VObject->serialnumber'";
		$s = mysql_query($query) or die(mysql_error());	 
		return $this->getAllStates();
	}
}

class VOCountryState{
	var $serialnumber;
	var $country_fk;
	var $country_name;
    var $state_name;
    var $cre_userid_fk;
    var $cre_datetime;
    var $activestatus;
}
class VOCountryCurrency {
    var $country_serialnumber;
	var $currency_serialnumber;
    var $country_name;
	var $currency_name;
	var $currency_value;	
    var $cre_userid_fk;
    var $cre_datetime;
    var $activestatus;
}

?>